﻿

namespace LS.Nc6.Application.System
{
    /// <summary>
    /// 提供字典管理服务
    /// </summary>

    public class SysDictionaryService : BaseService<SysDictionaryEntity> ,ISysDictionaryService, ITransient
    {
        
        #region 实现接口方法
        public async Task<int> DeleteType(string dicids)
        {
            var allIds= dicids.GetStringToLongArryNoNull();
            return await  DbContext.Instance.Deleteable<SysDictionaryEntity>().Where(w => allIds.Contains(w.Id) || allIds.Contains(w.ParentId)).IsLogic().ExecuteCommandAsync();
        }

        public async Task<string> DictionaryName(string dval, SysDictionaryType dicType)
        {
            string strditctype = dicType.ToString();
         var  firstmodel= await   DbContext.Instance.Queryable<SysDictionaryEntity>().Where(a => a.Isenable == true && a.Dvalue == dval &&
            a.ParentId == SqlFunc.Subqueryable<SysDictionaryEntity>().Where(s => s.Dictype == strditctype ).Select(s => s.Id)).FirstAsync(); //&& SqlFunc.IsNull(a.ParentId, 0) == 0

            return firstmodel != null ? firstmodel.Dname : String.Empty;
        }

        public async Task<List<SysDictionaryEntity>> DictionaryTagByDicType(SysDictionaryType dicType)
        {
            string strditctype = dicType.ToString();
            return  await DbContext.Instance.Queryable<SysDictionaryEntity>().Where(a => a.Isenable == true  &&
           a.ParentId == SqlFunc.Subqueryable<SysDictionaryEntity>().Where(s => s.Dictype == strditctype ).Select(s => s.Id)).OrderByDescending(a=>a.Dseq).ToListAsync(); //&& SqlFunc.IsNull(a.ParentId, 0) == 0
        }

        public async Task<int> ModifyState(long id, int state)
        {
            return await DbContext.Instance.Updateable<SysDictionaryEntity>().SetColumns(it => it.Isenable == state.ParseToBool()).ExecuteCommandAsync();
        }
        #endregion 实现接口方法
    }
}
